﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;


namespace API.Controllers
{
    [RoutePrefix("Api/UserCloudData")]
    public class UserCloudDataController : ApiController
    {
        B_UserCloudData bu;
        public UserCloudDataController()
        {
            bu = new B_UserCloudData();
        }

        /// <summary>
        /// 根据用户id获取模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("FindByUserID/{id}")]
        public IHttpActionResult FindByUserID(int id)
        {
            var user = new B_User();
            var userclouddata = new B_UserCloudData();
            var template = new B_Template();
            var data = from u in user.GetAll()
                       join ucd in userclouddata.GetAll()
                       on u.UserID equals ucd.UserID
                       join t in template.GetAll()
                       on ucd.TemplateID equals t.TemplateID
                       where u.UserID == id
                       select new Common.Param_UserCloudDataByUserID
                       {
                           UserID = u.UserID,
                           UCName = ucd.UCName,
                           UCWish = ucd.UCWish,
                           UCIMG = ucd.UCIMG,
                           UserName = u.UserName,
                           Tname = t.Tname,
                           TemplateID = t.TemplateID,
                       };
            return Ok(new Result<List<Param_UserCloudDataByUserID>> { Data = data.ToList() });
        }
        /// <summary>
        /// 获取所有用户云数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetData")]
        public IHttpActionResult GetData()
        {
            var data = from u in bu.GetAll()
                       select new UserCloudData
                       {
                           UCDID = u.UCDID,
                           UCName = u.UCName,
                           UCWish = u.UCWish,
                           UCIMG = u.UCIMG,
                           UserID = u.UserID,
                           TemplateID = u.TemplateID
                       };
            return Ok(new Result<List<UserCloudData>> { Data = data.ToList() });
        }

        /// <summary>
        /// 添加云数据用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(UserCloudData user)
        {
            if (bu.GetAll().Count(x => x.UCName == user.UCName) == 0)
            {
                var data = new UserCloudData
                {
                    UCName = user.UCName,
                    UCWish = user.UCWish,
                    UCIMG = user.UCIMG,

                    UserID = user.UserID,

                    TemplateID = user.TemplateID
                };
                bu.Add(data);
                return Ok(new Result<UserCloudData> { Data = data });
            }
            else
            {
                return Ok(new Result<object>
                {
                    Code = 403,
                    Msg = "用户已存在"
                });
            }
        }

        /// <summary>
        /// 删除云数据用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                bu.Delete(id);
                return Ok(new Result<object>
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "该用户已删除"
                });
            }
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = bu.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据丢了"
                    });
                }
                return Ok(new Result<UserCloudData>
                {
                    Data = new UserCloudData
                    {
                        UCName = data.UCName,
                        UCWish = data.UCWish,
                        UCIMG = data.UCIMG,

                        UserID = data.UserID,

                        TemplateID = data.TemplateID
                    }
                });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, UserCloudData user)
        {
            UserCloudData db = new UserCloudData
            {
                UCName = user.UCName,
                UCWish = user.UCWish,
                UCIMG = user.UCIMG,
                UserID = user.UserID,
                TemplateID = user.TemplateID
            };
            try
            {
                bu.Update(id, db);
                return Ok(new Result<object>());
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "更新失败"
                });
            }
        }
    }
}
